<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div fxLayout="column" fxFlex [ngClass]="{'border': isConnector}">
  <div fxLayout="row" fxLayout.lt-sm="column" class="command-form" fxLayoutGap="10px" [formGroup]="commandForm">
    <ng-container *ngIf="!isConnector; else connectorForm">
      <mat-form-field>
        <mat-label>{{ 'gateway.statistics.command' | translate }}</mat-label>
        <mat-select formControlName="command">
          <mat-option *ngFor="let command of RPCCommands" [value]="command">
            {{ command }}
          </mat-option>
        </mat-select>
      </mat-form-field>
      <mat-form-field fxFlex>
        <mat-label>{{ 'gateway.statistics.timeout-ms' | translate }}</mat-label>
        <input matInput formControlName="time" type="number" min="1"/>
        <mat-error *ngIf="commandForm.get('time').hasError('min')">
          {{ 'gateway.statistics.timeout-min' | translate }}
        </mat-error>
      </mat-form-field>
      <button mat-raised-button
              color="primary"
              (click)="sendCommand()"
              [disabled]="commandForm.invalid">
        {{ 'gateway.rpc-command-send' | translate }}
      </button>
    </ng-container>
    <ng-template #connectorForm>
      <tb-gateway-service-rpc-connector
        *ngIf="connectorType !== ConnectorType.MODBUS else modbusParameters"
        formControlName="params"
        [connectorType]="connectorType"
        (sendCommand)="sendCommand()"
        (saveTemplate)="saveTemplate()"
      />
      <ng-template #modbusParameters>
        <div fxLayout="column" class="rpc-parameters">
          <div class="mat-subtitle-1 tb-form-panel-title">{{ 'gateway.rpc.title' | translate: {type: gatewayConnectorDefaultTypesTranslates.get(connectorType)} }}</div>
          <tb-modbus-rpc-parameters formControlName="params"/>
          <div class="template-actions" fxFlex fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="10px">
            <button mat-raised-button
                    (click)="saveTemplate()"
                    [disabled]="commandForm.get('params').invalid">
              {{ 'gateway.rpc-command-save-template' | translate }}
            </button>
            <button mat-raised-button
                    color="primary"
                    (click)="sendCommand()"
                    [disabled]="commandForm.get('params').invalid">
              {{ 'gateway.rpc-command-send' | translate }}
            </button>
          </div>
        </div>
      </ng-template>
    </ng-template>
  </div>
  <section class="result-block" [formGroup]="commandForm">
    <span>{{ 'gateway.rpc-command-result' | translate }}
      <div *ngIf="resultTime" class="result-time" fxFlex fxLayout="row" fxLayoutAlign="center center">
        <mat-icon class="material-icons">schedule</mat-icon>
        <span>{{ resultTime | date: 'yyyy/MM/dd HH:mm:ss' }}</span>
      </div>
    </span>
    <tb-json-content [contentType]="contentTypes.JSON" readonly="true" formControlName="result"></tb-json-content>
  </section>
</div>
<tb-gateway-service-rpc-connector-templates fxFlex="30" *ngIf="isConnector" class="border" [rpcTemplates]="templates"
                                            [ctx]="ctx" [connectorType]="connectorType" (useTemplate)="useTemplate($event)">
</tb-gateway-service-rpc-connector-templates>
